package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FundInfoEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FundInfoDetailProcessor implements PageProcessor {

	private static final String baseUrl = "http://gs.amac.org.cn/amac-infodisc/res/pof/fund/";
	private static final String topUrl = "http://gs.amac.org.cn/amac-infodisc/res/pof/";
	
	private static Logger logger = Logger.getLogger(FundInfoDetailProcessor.class);
	
	private static AtomicInteger atomicInteger = new AtomicInteger(0);
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		JSONObject ctxObj = (JSONObject)page.getRequest().getExtParams().get("ctxObj");
		FundInfoEntity fundInfo = new FundInfoEntity();
		fundInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		Element table = doc.select("table[class=\"table table-center table-info\"] > tbody").first();
		fundInfo.setFundName(table.getElementsMatchingOwnText("基金名称:").first().nextElementSibling().text());//基金名称
		fundInfo.setFundNo(table.getElementsMatchingOwnText("基金编号:").first().nextElementSibling().text());//基金编号
		String establishDate = table.getElementsMatchingOwnText("成立时间:").first().nextElementSibling().text();//成立时间
		fundInfo.setEstablishDate(DateUtils.parseDate(establishDate));
		String putonRecordDate = table.getElementsMatchingOwnText("备案时间:").first().nextElementSibling().text();//备案时间
		fundInfo.setPutonRecordDate(DateUtils.parseDate(putonRecordDate));
		fundInfo.setPutonRecordStage(table.getElementsMatchingOwnText("基金备案阶段:").first().nextElementSibling().text());//基金备案阶段
		fundInfo.setFundType(table.getElementsMatchingOwnText("基金类型:").first().nextElementSibling().text());//基金类型
		fundInfo.setCurrency(table.getElementsMatchingOwnText("币种:").first().nextElementSibling().text());//币种
		fundInfo.setManagerName(table.getElementsMatchingOwnText("基金管理人名称:").first().nextElementSibling().text());//基金管理人名称
		fundInfo.setManagerType(table.getElementsMatchingOwnText("管理类型:").first().nextElementSibling().text());//管理类型
		fundInfo.setCustodian(table.getElementsMatchingOwnText("托管人名称:").first().nextElementSibling().text());//托管人名称
		if(table.getElementsMatchingOwnText("主要投资领域:")!=null){
			if(table.getElementsMatchingOwnText("主要投资领域:").first()!=null){
//				table.getElementsMatchingOwnText("主要投资领域:").first().nextElementSibling().text();
				fundInfo.setInvestmentAreas(table.getElementsMatchingOwnText("主要投资领域:").first().nextElementSibling().text());//主要投资领域
			}
			
		}
//		fundInfo.setInvestmentAreas(table.getElementsMatchingOwnText("主要投资领域:").first().nextElementSibling().text());//主要投资领域
		fundInfo.setWorkingState(table.getElementsMatchingOwnText("运作状态:").first().nextElementSibling().text());//运作状态
		String lastUpdateTime = table.getElementsMatchingOwnText("基金信息最后更新时间:").first().nextElementSibling().text();//基金信息最后更新时间
		fundInfo.setLastUpdateTime(DateUtils.parseDate(lastUpdateTime));
		fundInfo.setSpecialNotice(table.getElementsMatchingOwnText("基金协会特别提示").first().nextElementSibling().text());//基金协会特别提示（针对基金）
		
		fundInfo.setId(ctxObj.getString("id"));
		fundInfo.setIsDeputeManage(ctxObj.getString("isDeputeManage"));
		fundInfo.setLastQuarterUpdate(ctxObj.getString("lastQuarterUpdate"));
		fundInfo.setManagerUrl(ctxObj.getString("managerUrl").replace("../", topUrl));
		fundInfo.setUrl(baseUrl.concat(ctxObj.getString("url")));
		
		if(table.getElementsMatchingOwnText("信息披露情况").size()>0){
			fundInfo.setMonthlyReport(table.getElementsMatchingOwnText("月报:").first().nextElementSibling().text());
			fundInfo.setSemiAnnualReport(table.getElementsMatchingOwnText("半年报:").first().nextElementSibling().text());
			fundInfo.setAnnualReport(table.getElementsMatchingOwnText("年报:").first().nextElementSibling().text());
			fundInfo.setQuarterly(table.getElementsMatchingOwnText("季报:").first().nextElementSibling().text());
		}
		fundInfo.setId(UuIdPublic.timeStrongID(5));
		StringUtils.removeSpace(fundInfo);
		objectContainer.add(fundInfo);
		int count = atomicInteger.incrementAndGet();
		String pageIndex = (String)page.getRequest().getExtParams().get("pageIndex");
		logger.info("当前第"+pageIndex+"页,解析第"+count+"条数据完成:{"+fundInfo.getFundNo()+":"+fundInfo.getFundName()+"}");
	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		
	}

}
